package com.apabi.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.apabi.common.pojo.JqGridResult;
import com.apabi.mapper.${entity.className}Mapper;
import com.apabi.pojo.${entity.className};
import com.apabi.service.${entity.className}Service;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by zhangxh.apabi on 2017/9/22.
 */
@Service
public class ${entity.className}ServiceImpl implements ${entity.className}Service{


    @Autowired
    private ${entity.className}Mapper ${entity.classInstanceName}Mapper;

    @Autowired
    private Sid sid;

    @Override
    public void save${entity.className}(${entity.className} ${entity.classInstanceName}) {
        String itemId = sid.nextShort();
        ${entity.classInstanceName}.setId(itemId);
        ${entity.classInstanceName}Mapper.insert(${entity.classInstanceName});
    }

    @Override
    public JqGridResult query${entity.className}List(String name, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<${entity.className}> list =  ${entity.classInstanceName}Mapper.selectAll();
        PageInfo<${entity.className}> pageList = new PageInfo<${entity.className}>(list);

        JqGridResult grid = new JqGridResult();
        grid.setTotal(pageList.getPages());
        grid.setRows(list);
        grid.setPage(pageList.getPageNum());
        grid.setRecords(pageList.getTotal());
        return grid;
    }

    @Override
    public ${entity.className} query${entity.className}ById(String ${entity.classInstanceName}Id) {
        return ${entity.classInstanceName}Mapper.selectByPrimaryKey(${entity.classInstanceName}Id);
    }

    @Override
    public void delete${entity.className}ById(String ${entity.classInstanceName}Id) {
        ${entity.classInstanceName}Mapper.deleteByPrimaryKey(${entity.classInstanceName}Id);
    }

    @Override
    public void update${entity.className}ById(${entity.className} ${entity.classInstanceName}) {
        ${entity.classInstanceName}Mapper.updateByPrimaryKey(${entity.classInstanceName});
    }
}
